//
//  YWAddViewController.m
//  网易新闻
//
//  Created by Yeewii@hotmail.com on 16/6/15.
//  Copyright © 2016年 Yeewii@hotmail.com. All rights reserved.
//

#import "YWAddViewController.h"
#import "YWHttpTool.h"
#import "YWAddModelNSObject.h"
#import <UIImageView+WebCache.h>
#import <UIView+SDAutoLayout.h>
#import "YWAddModelImgs.h"
#import "ViewController.h"
#import "YWTabBarController.h"

@interface YWAddViewController ()

@end

@implementation YWAddViewController
{
    UIImageView *_bgImage;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpViews];
    [self requestData];
    
    
}

-(void)setUpViews
{
    _bgImage = [UIImageView new];
    [self.view addSubview:_bgImage];
    _bgImage.sd_layout
    .spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0));
    
}

-(void)requestData
{
    __weak typeof(self) weakSelf = self;
    [YWHttpTool GET:ADDURL params:nil success:^(NSDictionary *responseObject) {
        
        YWAddModelNSObject *addModel = [YWAddModelNSObject modelObjectWithDictionary:responseObject];
        NSArray *imgArr = addModel.imgs;
        
        NSMutableArray *picArr = [NSMutableArray array];
        for (YWAddModelImgs *imgMod in imgArr)
        {
            if ([imgMod.date isEqualToString:[weakSelf getDateInfo]])
            {
                [picArr addObject:imgMod.url[0]];
                NSLog(@"%@",imgMod.url[0][0]);
                [_bgImage sd_setImageWithURL:[NSURL URLWithString:imgMod.url[0][0]]];
                break;
            }
        }
        
    } failure:^(NSError *error) {
        
    }];
}


#pragma mark - 获取日期 -
- (NSString *) getDateInfo
{
    NSDate * date  = [NSDate date];
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date];
    NSString *dateString = [NSString stringWithFormat:@"%ld-%.2ld-%ld",comps.year,comps.month,comps.day];
    return dateString;
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.view.window.rootViewController = [YWTabBarController new];
}

@end
